﻿using System;
using System.Web.Services;

using Cognifide.PowerShell.Shell.Settings;

namespace Cognifide.PowerShell.Console.Controls
{
    public partial class Console : System.Web.UI.UserControl
    {
        private PsConsoleSettings Settings { get { return PsConsoleSettings.Instance; } }

        public string Script
        {
            get { return txbCodePart2.Text; }
            set { txbCodePart2.Text = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            executeScript.Click += new EventHandler(btnExecute_Click);
        }


        public void btnExecute_Click(object sender, EventArgs e)
        {
            //ExecutionControl.Script = String.Format("{0}\n{1}", ConsoleParent.SettingsEditor.PreScript, Script);
            ExecutionControl.Script = Script;
            ExecutionControl.Execute();

            if (Settings.SaveLastScript)
            {
                Settings.Load();
                Settings.LastScript = txbCodePart2.Text;
                Settings.Save();
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack)
            {
                if (Settings.SaveLastScript)
                {
                    Settings.Load();
                    Script = Settings.LastScript;
                }
            }
        }


    }
}
